# coding:utf-8
# import os
# import subprocess
# import json
# import MySQLdb
#
#
# def getconn(database):
#     try:
#         conn = MySQLdb.connect(host=database[0], port=database[1], user=database[2], passwd=database[3], db=database[4],
#                                charset='utf8')
#         return conn
#     except Exception, e:
#         print 'error'
#
#
# def getcommit(conn):
#     try:
#         conn.commit()
#         conn.close()
#     except Exception, e:
#         print 'error'
#
#
# def execute_many(sql, param, database):
#     try:
#         conn = getconn(database=database)
#         cursor = conn.cursor()
#         cursor.executemany(sql, param)
#         getcommit(conn)
#     except Exception, e:
#         print 'mysql error', e
#
#
# path = 'daily/2016/10/12'
# database = ('192.168.211.67', 3406, 'ppdcuser_credit', 'Ppda1@.41ConnPDa@d$963%', 'test')
# json_list = []
# sql = 'insert into phonebills_top100_etl(json_object,userId,phoneNo,token,timestamp,date) VALUES (%s,%s,%s,%s,%s,%s)'
# for p in os.walk(path):
#     if p[2]:
#         for f in p[2]:
#             if '.snappy' not in f and os.path.exists(p[0] + '/' + f + '.snappy'):
#                 print 'file:', p[0] + '/' + f
#                 for line in open(p[0] + '/' + f, 'r'):
#                     json_result = json.loads(line)
#                     userid = json_result['userId'] if 'userId' in json_result.keys() else None
#                     phoneNo = json_result['phoneNo'] if 'phoneNo' in json_result.keys() else None
#                     token = json_result['token'] if 'token' in json_result.keys() else None
#                     timestamp = json_result['timestamp'] if 'timestamp' in json_result.keys() else None
#                     date = p[0].replace('daily/', '')
#                     json_list.append((line, userid, phoneNo, token, timestamp, date))
#                 execute_many(sql, json_list, database)
#             elif '.snappy' in f:
#                 continue
#             else:
#                 print 'not exsit:', p[0] + '/' + f + '.snappy'
#                 # os.popen('hadoop fs -text file:/home/dp/' + p[0] + '/' + f + ' >> /home/dp/' + p[0] + '/' + f.replace('.snappy',''))
# import this
# print this.s
# import codecs
# import json
#
# f1_new = codecs.open('blacklist_person.txt', 'r')
# for i, line in enumerate(f1_new):
#     res = line.strip().split('\t')
#     print '\t'.join(res)

# import happybase

#
# name = 'xfiles_map_id2other'
# connection = happybase.Connection('hadoop28.bs.ppcredit.com')
# table = connection.table('adl:' + name)
# f = open('/home/dp/' + name, 'r')
# with table.batch() as b:
#     for line in f:
#         b.delete(line.strip())
# connection = happybase.Connection('hadoop27.sc.ppcredit.com')
# table = connection.table('tdl:test_info1')
#
# table.put('1-3', {'info:user_type': '1', 'info:gender': 'F', 'info:birthday': '1998-10-12',})
# for row,info in table.scan():
#     print row,info
# print [1]*10
# table = connection.table('adl:xfiles_map_jdusername2id')
# row=table.row('A++JdwEjhH4v9cCHLfSmRjBHiuKGXL8vmGdl+6MG33jQHgAipYNNrN2n5TaWvYYpdarAJtMjIKCQPss13lV4MokEfuwgqiciK5a3sBebiSgSUzjThkMpz8rGkpFJCnEeSxyDwuzhQKyug4ZWDVskoAu6TLquDQ4ryqgOFsKG6BY=')
# if row:
#     print row
# else:
#     print 'error'
# for rowkey, data in table.scan(
#         row_start='A++JdwEjhH4v9cCHLfSmRjBHiuKGXL8vmGdl+6MG33jQHgAipYNNrN2n5TaWvYYpdarAJtMjIKCQPss13lV4MokEfuwgqiciK5a3sBebiSgSUzjThkMpz8rGkpFJCnEeSxyDwuzhQKyug4ZWDVskoAu6TLquDQ4ryqgOFsKG6BY=|0',
#         row_stop='A++JdwEjhH4v9cCHLfSmRjBHiuKGXL8vmGdl+6MG33jQHgAipYNNrN2n5TaWvYYpdarAJtMjIKCQPss13lV4MokEfuwgqiciK5a3sBebiSgSUzjThkMpz8rGkpFJCnEeSxyDwuzhQKyug4ZWDVskoAu6TLquDQ4ryqgOFsKG6BY=|9'):
#     print rowkey

# for key, data in table.rows([b'row-key-1', b'row-key-2']):
#     print(key, data)  # prints row key and data for each row
#
# for key, data in table.scan(row_prefix=b'row'):
#     print(key, data)  # prints 'value1' and 'value2'
#
# row = table.delete(b'row-key')
# import time
# print int(time.strftime("%Y"))-1988
# 130481198810310015

# import base64
#
# import requests
#
#
# with open("123.jpg", "rb") as f:
#     pic1 = f.read()
# with open("456.jpg", "rb") as f:
#     pic2 = f.read()
#
# challenge = "123456789012345678901234567890qwadaedfqwefwe"
#
# url = 'http://dama.istarshine.com/geetest'
#
# #case 1
# data = {
#     "pic1":base64.encodestring(pic1),
#     "pic2":base64.encodestring(pic2),
#     "challenge":challenge,
#     "token": "4e20c5d9f8cb63bb2f8710aed38f6286",
#     }
#
# resp = requests.post(url, data=data)
# print resp.content

# import this
# import time
# dt = int(time.strftime("%Y",time.localtime(int('1481236541'))))-1988
# print dt

# import time
# from datetime import datetime, timedelta

# import copy
# option = {
#     'grid': {
#         'top': '10%',
#         'left': '2%',
#         'right': '3%',
#         'bottom': '0%',
#         'containLabel': True
#     },
#     'legend': {
#         'x': 'center',
#         'data': [],
#     },
#     'tooltip': {
#         'trigger': 'axis',
#     },
#     'xAxis': {
#         'type': 'category',
#         'boundaryGap': False,
#         'data': []
#     },
#     'yAxis': {
#         'type': 'value',
#         'name': 'Mb/s'
#     },
#     'series': [
#
#     ]
# }
# # cm_host = "172.19.100.12"
# from cm_api.api_client import ApiResource
# import requests
# import json

# possible status:
# UNKNOWN
# NONE
# STOPPED
# DOWN
# UNKNOWN_HEALTH
# DISABLED_HEALTH
# CONCERNING_HEALTH
# BAD_HEALTH
# GOOD_HEALTH
# STARTING
# STOPPING
# HISTORY_NOT_AVAILABLE

# version = 12
# cluster_name = "Cluster 1"
#
# # ***online***
# cm_host = "172.19.100.12"
# username = "dp"
# password = "dp_ppc"
# kafka_brokers = {'kafka-KAFKA_BROKER-689e43e63563a6772803dceae726f054': 'hadoop20.bs.ppcredit.com',
#                  'kafka-KAFKA_BROKER-b3802d188bf789813cc723c0d80617f8': 'hadoop28.bs.ppcredit.com',
#                  'kafka-KAFKA_BROKER-b954f61ef4f1ff42b0668001f103833e': 'hadoop19.bs.ppcredit.com'}

# ***offline***
# cm_host = "192.168.213.23"
# username = "shen"
# password = "shen"
# kafka_brokers = {'kafka-KAFKA_BROKER-1aeddd6569c48cbf4de64dcc718220ef': 'hadoop26.sc.ppcredit.com',
#                  'kafka-KAFKA_BROKER-6da0128f4b8fae8dcf74f30bd593ecfe': 'hadoop25.sc.ppcredit.com',
#                  'kafka-KAFKA_BROKER-68bebe6cdfb2bb675506f86c8465d64c': 'hadoop31.sc.ppcredit.com'}

# def send_msg_to_wechat(msg):
#     datas = '{"toparty": 5, "msgtype": "text", "agentid": 1, "text": {"content": "%s"}, "safe": 0}' % msg
#     url_token = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx9c595e2ff0429f13&corpsecret=0muEXbvPoOZeR_o7hkd3fdt5vBh5o0-uFIKgl4LDRzA-jBTXx7qgKtRO668D-VT6'
#     url_send = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s'
#     resp = requests.get(url_token)
#     access_token = json.loads(resp.content)['access_token']
#     header = {'Accept': 'application/json'}
#     resp = requests.post(url_send % access_token, data=datas, headers=header)
#     print resp.content
#
#
# def get_cdh():
#     api = ApiResource(cm_host, username=username, password=password, version=version)
#     cdh5 = api.get_cluster(cluster_name)
#     return cdh5
#
#
# def restart_kafka_error_status():
#     cdh5 = get_cdh()
#     kafka = cdh5.get_service('kafka')
#     roles = kafka.get_all_roles()
#     error = []
#     for role in roles:
#         status = role.entityStatus
#         if status not in ['GOOD_HEALTH', 'STARTING', 'CONCERNING_HEALTH']:
#             error.append(role.name)
#         else:
#             print 'kafka:', kafka_brokers[role.name], 'status ok!'
#     for name in error:
#         msg = 'restarting kafka broker at %s' % kafka_brokers[name]
#         kafka.restart_roles(name)
#         send_msg_to_wechat(msg)
#
#
# def restart_hbase_error_status():
#     cdh5 = get_cdh()
#     hbase = cdh5.get_service('hbase')
#     roles = hbase.get_all_roles()
#     error = []
#     for role in roles:
#         if role.type != "REGIONSERVER":
#             continue
#         status = role.entityStatus
#         if status not in ['GOOD_HEALTH', 'STARTING', 'CONCERNING_HEALTH']:
#             error.append(role.name)
#         else:
#             print 'hbase:', role.name, 'status ok!'
#     for name in error:
#         msg = 'restarting hbase region server at %s' % name
#         hbase.restart_roles(name)
#         send_msg_to_wechat(msg)
#
#
# restart_hbase_error_status()
# restart_kafka_error_status()

# list_ = yarn.get_yarn_applications(start_time=(datetime.now() - timedelta(minutes=1)), end_time=datetime.now(),
#                                    filter_str='pool=streaming and user=dp')
# application_list = {}
# for item in list_.applications:
#     name = item.name
#     starttime = item.startTime.strftime('%Y-%m-%d %H:%M')
#     applicationid = item.applicationId
#     state = item.state
#     if name not in application_list.keys():
#         application_list[name] = [starttime, applicationid, state]

# a = 1
# b = 2
# print locals()
# print globals()
#
#
# def foo(arg, a):
#     x = 1
#     y = 'xxxxxx'
#     for i in range(10):
#         j = 1
#         k = i
#     print locals()
#     print globals()
#
#
# print foo(*[1, 2])
# my_very_big_string = (
#     "For a long time I used to go to bed early. Sometimes, "
#     "when I had put out my candle, my eyes would close so quickly "
#     "that I had not even time to say “I’m going to sleep.”"
# )
# print type(my_very_big_string)
# import requests
# from flask.signals import Namespace
#
# my_signals = Namespace()
# model_saved = my_signals.signal('model-xxx')
#
# print model_saved.name

# from pyhive import hive
# from TCLIService.ttypes import TOperationState
# cursor = hive.connect('hadoop24.bs.ppcredit.com',port=10000).cursor()
# cursor.execute('show tables', async=True)
#
# status = cursor.poll().operationState
# while status in (TOperationState.INITIALIZED_STATE, TOperationState.RUNNING_STATE):
#     logs = cursor.fetch_logs()
#     for message in logs:
#         print message
#
#     # If needed, an asynchronous query can be cancelled at any time with:
#     # cursor.cancel()
#     cursor.cancel():quit()
#     status = cursor.poll().operationState
#
# print cursor.fetchall()

# import requests
# from bs4 import BeautifulSoup

# url = 'http://172.19.100.23:7070//kylin/api/cubes/cube_phonebills_analysis/build'
# segments = [(1488326400000, 1493596500000)]
# json_format = {'startTime': None, 'endTime': None, 'buildType': 'REFRESH'}
# header = {'Authorization': 'Basic QURNSU46S1lMSU4=', 'Content-Type': 'application/json;charset=UTF-8'}
# for times in segments:
#     current_json = json_format
#     current_json['startTime'], current_json['endTime'] = times[0], times[1]
#     resp = requests.put(url, data=header, headers=header)
#     print resp.status_code
# t = 4386
# s_dict = {}
# f = open('fck.txt', 'r')
# for line in f:
#     line = line.strip()
#     if line[:6] in s_dict:
#         s_dict[line[:6]].append(line)
#     else:
#         s_dict[line[:6]] = [line]
# t = 5609
# f = open('fck.txt', 'r')
# s_dict = []
# for line in f:
#     s_dict.append(line.strip())
#
# url = 'http://qq.ip138.com/idsearch/index.asp?action=idcard&userid=xxx&B1=%B2%E9+%D1%AF'
# header = {'Host': 'qq.ip138.com',
#           'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',
#           'Referer': 'http://qq.ip138.com/idsearch/index.asp'}
# for key in s_dict:
#     current_url = url.replace('xxx', key)
#     resp = requests.get(current_url, headers=header)
#     if resp.status_code == 200:
#         soup = BeautifulSoup(resp.content, "html.parser")
#         tds = soup.find_all('td', class_='tdc2')
#         if len(tds) >= 3:
#             location = tds[2].text.strip().split(' ')
#             if '' in location:
#                 location.remove('')
#             print t, ',', key[:6], ',', location[-1], ',', '|'.join(location)
#         else:
#             print t, ',', key[:6]
#         t += 1

# import codecs
#
# f = codecs.open('fck.txt', 'r', 'utf8')
# f_new = codecs.open('new_fck.txt', 'r', 'utf8')
# location = []
# for line in f_new:
#     line = line.strip().split(',')
#     location.append(line)
# for line in f:
#     flag = 0
#     line = line.strip().split(',')
#     dest = line[3].split("|")
#     if '' in dest:
#         dest.remove('')
#     dest = [item.replace(u'地区', '').replace(u'县', '').replace(u'区', '') for item in dest]
#     for l in location:
#         if l[2][-1]==u'省':
#             continue
#         if l[2].find(dest[-1])==0:
#             flag = 1
#             print line[0], ',', line[1], ',', line[2], ',', l[3], ',', '|'.join(
#                 l[4].split('|')[:-1]+[line[1]]), ',', l[5]
#             break
#     if flag == 1:
#         continue
#     for l in location:
#         if l[2][-1]==u'省':
#             continue
#         if len(dest)>1 and l[2].find(dest[-2])==0:
#             flag = 1
#             print line[0], ',', line[1], ',', line[2], ',', l[3], ',', '|'.join(
#                 l[4].split('|')[:-1]+[line[1]]), ',', l[5]
#             break
#     if flag == 1:
#         continue
#     elif flag == 0:
#         print line[0], ',', line[1], ',', line[2],',',line[3]
# a=[{},{},{},{}]
# b=[1,2,3]
# c=zip(a,b)
# for t in c:
#     print t[0].strip()+'\001'+t[1].strip()
# f_l=open('mobilephone_location.txt')
# f_n=open('ppc_phone.txt')
# p_l={}
#
# for line in f_l:
#     line=line.strip().split(',')
#     p_l[line[0]]=line
# for line in f_n:
#     if not line.strip():
#         continue
#     line=line.strip().split('\t')
#     if line[1] not in p_l:
#         print line[1],line[2],line[3],line[4]
# import codecs
#
# cities = codecs.open('city.txt', 'r', 'utf8')
# city = {}
# city1 = {}
# for c in cities:
#     c = c.strip().split(',')
#     city[c[0]] = c[1]
#     city1[c[2]] = c[0]
# provices = codecs.open('province.txt', 'r', 'utf8')
# provice = {}
# for c in provices:
#     c = c.strip().split(',')
#     provice[c[0]] = c[1]
#
# operator = {'联通': '1', '移动': '2', '电信': '3', '虚拟/联通': '4', '虚拟/移动': '5', '虚拟/电信': '6'}
# f_n = codecs.open('ppc_phone.txt', 'r', 'utf8')
# phone_n = {}
# for line in f_n:
#     if not line.strip():
#         continue
#     line = line.strip().split('\t')
#     phone_n[line[1]] = line
#
# f_l = codecs.open('mobilephone_location.txt', 'r', 'utf8')
# f_r = codecs.open('result.txt', 'a', 'utf8')
# for line in f_l:
#     line = line.strip().split(',')
#     operator_n = phone_n[line[0]][4] + phone_n[line[0]][8]
#     if operator_n.find('虚拟') > -1:
#         if operator_n.find('联通') > -1:
#             operator_n = '4'
#         elif operator_n.find('移动') > -1:
#             operator_n = '5'
#         elif operator_n.find('电信') > -1:
#             operator_n = '6'
#     else:
#         if operator_n.find('联通') > -1:
#             operator_n = '1'
#         elif operator_n.find('移动') > -1:
#             operator_n = '2'
#         elif operator_n.find('电信') > -1:
#             operator_n = '3'
#     if provice[city[line[1]]] != phone_n[line[0]][2]:
#         if line[2] != operator_n:
#             print line[0], provice[city[line[1]]], phone_n[line[0]][2], line[2], operator_n
#         else:
#             if phone_n[line[0]][3] in city1:
#                 f_r.write(line[0] + ',' + city1[phone_n[line[0]][3]] + ',' + line[2] + '\n')
#             else:
#                 print line[0] + ',' + phone_n[line[0]][3] + ',' + line[2]
#     else:
#         if line[2] != operator_n:
#             f_r.write(line[0] + ',' + line[1] + ',' + operator_n + '\n')
#         else:
#             f_r.write(line[0] + ',' + line[1] + ',' + line[2] + '\n')
#     f_r.flush()
# !/usr/bin/python
# _*_coding:utf-8 _*_

# import urllib, urllib2
# import json
# import sys
# import simplejson
#
# reload(sys)
# sys.setdefaultencoding('utf-8')
#
#
# def gettoken(corpid, corpsecret):
#     gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + ' & corpsecret = ' + corpsecret
#     print  gettoken_url
#     try:
#         token_file = urllib2.urlopen(gettoken_url)
#     except urllib2.HTTPError as e:
#         print e.code
#         print e.read().decode("utf8")
#         sys.exit()
#     token_data = token_file.read().decode('utf-8')
#     token_json = json.loads(token_data)
#     token_json.keys()
#     token = token_json['access_token']
#     return token
#
#
# def senddata(access_token, user, subject, content):
#     send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
#     send_values = {
#         "touser": user,  # 企业号中的用户帐号，在zabbix用户Media中配置，如果配置不正常，将按部门发送。
#         "toparty": "5",  # 企业号中的部门id。
#         "msgtype": "text",  # 消息类型。
#         "agentid": "1",  # 企业号中的应用id。
#         "text": {
#             "content": subject + '\n' + content
#         },
#         "safe": "0"
#     }
# #    send_data = json.dumps(send_values, ensure_ascii=False)
# send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')
# send_request = urllib2.Request(send_url, send_data)
# response = json.loads(urllib2.urlopen(send_request).read())
# print str(response)

# if __name__ == '__main__':
#     user = str(sys.argv[1])  # zabbix传过来的第一个参数
#     subject = str(sys.argv[2])  # zabbix传过来的第二个参数
#     content = str(sys.argv[3])  # zabbix传过来的第三个参数
#
#     corpid = 'wx9c595e2ff0429f13'  # CorpID是企业号的标识
#     corpsecret = 'Ois1toLLjFaJnIN6RppvSafxjSvs_USRKEz826j1VrY'  # corpsecretSecret是管理组凭证密钥
#     accesstoken = gettoken(corpid, corpsecret)
#     senddata(accesstoken, user, subject, content)

# def send_msg_to_wechat(msg):
#     datas = {"toparty": "5", "msgtype": "text", "agentid": "1", "text": {"content": msg}, "safe": "0"}
#     url_token = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx9c595e2ff0429f13&corpsecret=Ois1toLLjFaJnIN6RppvSafxjSvs_USRKEz826j1VrY'
#     url_send = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s'
#     resp = requests.get(url_token)
#     access_token = json.loads(resp.content)['access_token']
#     header = {'Accept': 'application/json'}
#     resp = requests.post(url_send % access_token, data=json.dumps(datas), headers=header)
#     print resp.content
#
# send_msg_to_wechat('xxx')

# import requests
# file_str = 'tmp.pdf'
# resp = requests.get(
#     'http://172.19.100.26:8787/pentaho/api/repo/files/%3Apublic%3Aresult%3Areport_daily%3Areport_daily_20170706.pdf',
#     stream=True,
#     headers={'Authorization': 'Basic YWRtaW46YWRtaW4='})
# print resp.status_code
# with open(file_str, 'wb') as fd:
#     for chunk in resp.iter_content(512):
#         fd.write(chunk)
# import datetime
# print (datetime.datetime.now()-datetime.timedelta(days=1)).strftime('%Y%m%d')
import time
import random


# def stupid_fib(n):
#     index = n
#     a = 0
#     b = 1
#     while index:
#         sleep_cnt = yield b
#         print('let me think {0} secs'.format(sleep_cnt))
#         time.sleep(sleep_cnt)
#         a, b = b, a + b
#         index -= 1


# print '-' * 10 + 'test yield send' + '-' * 10
# N = 20
# sfib = stupid_fib(N)
# fib_res = sfib.send(None)
# while True:
#     print(fib_res)
#     try:
#         fib_res = sfib.send(random.uniform(0, 0.5))
#     except StopIteration:
#         break

